<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;

class Package extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = Db::name('customer_package');

    }

    public function signatureContract($package_id = '')
    {
        $package = $this->model->where(['package_id' => $package_id])->find();
        if (!$package || !$package['contract_url']) {
            echo "合同文件不存在";
            die;
        }
        $this->assign('package', $package);
        return $this->view->fetch();
    }

    public function details($package_id = '')
    {
        $package = $this->model->where(['package_id' => $package_id])->find();
        if (!$package || !$package['contract_url']) {
            echo "合同文件不存在";
            die;
        }
        $this->assign('package', $package);
        return $this->view->fetch();
    }

    public function saveImage($image, $package_id)
    {
        $signature_url = saveBase64($image);
        $package = $this->model->where(['package_id' => $package_id])->find();
        if (!$package || !file_exists($package->contract_url)) {
            echo "合同文件不存在";
            die;
        }
        $package->save([
            'signature_url' => $signature_url,
            'signature_time' => time()
        ]);
        $package->generateContract();
        return $this->success('提交成功');
    }

}
